class MicropostsController < ApplicationController
	before_action :signed_in_user,only:[:create,:destroy]
  before_action :correct_user,only:[:destroy]
  	
  	def create
  		@micropost = current_user.microposts.build(micropost_params)
      wraped = wrap(@micropost.content)
      @micropost.content = wraped
      
  		if @micropost.save
  			flash[:success] = "Micropost created!"
  			redirect_to root_url
  		else
        @feed_items = current_user.feed.paginate(page:params[:page],per_page:5)
  			render 'static_pages/home'
  		end
  	end
	
	def destroy
    #p "*"*10 + session[:return_to] + "*"*10
    @micropost.destroy
    #redirect_to root_url
    redirect_back_or root_path
	end

	private

    def correct_user
      @micropost = current_user.microposts.find_by(id:params[:id])
      redirect_to root_url if @micropost.nil?
    end

		def micropost_params
			params.require(:micropost).permit(:content)
		end 
end